package com.qdxwx.data.service.book;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.book.BookMapper;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.book.BookQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class BookServiceImpl  implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public int insert(Book book) {
        book.setUpdateTime(LocalDateTime.now());
        book.setCreateTime(LocalDateTime.now());
        return bookMapper.insertBook(book);
    }

    @Override
    public Book get(int id) {
        if(id < 1) return null;
        return bookMapper.selectById(id);
    }

    @Override
    public List<Book> getListByTeacherId(int teacherId, PageRequest page) {
        return bookMapper.selectListByTeacherId(teacherId,page);
    }

    @Override
    public int countByTeacherId(int teacherId) {
        return bookMapper.selectCountByTeacherId(teacherId);
    }

    @Override
    public List<Book> getList(BookQuery book, PageRequest page) {
        return bookMapper.selectList(book,page);
    }

    @Override
    public int count(BookQuery book) {
        Integer row = bookMapper.selectCount(book);
        return row != null?row:0;
    }

    @Override
    public int update(Book book) {
        if(book.getId() < 1) return 0;
        book.setUpdateTime(LocalDateTime.now());
        return bookMapper.updateBook(book);
    }

    @Override
    public int deleteById(int id) {
        if(id < 1) return 0;
        return bookMapper.deleteById(id);
    }

    @Override
    public int updateView(int id) {
        if(id < 1) return 0;
        return bookMapper.updateView(id);
    }

    @Override
    public int updateBuy(int id) {
        if(id < 1) return 0;
        return bookMapper.updateBuy(id);
    }
}
